package com.museum.data;

import java.util.HashMap;
import java.util.Map;

import android.util.Log;

import com.museum.model.Event;
import com.museum.model.Location;
import com.museum.model.Mp_Places_News;
import com.museum.model.Open_Date;
import com.museum.model.Open_Date_Item;
import com.museum.model.Place;
import com.museum.model.Visited;
import com.museum.model.Watched;

public class Mp_Places_News_Handler {
	private Mp_Places_News place_news;
	private Place place_info;
	private Event event;
	
	private String _place_id;
	
	XmlRpc xmlrpc = new XmlRpc();
	public Mp_Places_News_Handler(String place_id){
		_place_id = place_id;	
		place_news = new Mp_Places_News();
		this.Parse();		
	}
	public Mp_Places_News get_Mp_Places_News(){		
		return place_news;
	}
	private void Parse(){	
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("place_id", _place_id);
      	
      	Map<String, Object> response = xmlrpc.call("mp.places.news", params);
		
		for (Map.Entry<String, Object> entry : response.entrySet()) {
            String key = entry.getKey();
            Object [] value = (Object[])entry.getValue();	
            //Log.d("TAG","key: " + key + ", Value: " + value.toString());
            //Object[] resultProper = (Object[])value; 
            //Log.d("TAG"," Value: " + resultProper[0].toString());
            if(key.equals("news")){
            	for(int i = 0;i<value.length;i++){
            		ParsePlace_News(value[i]);
            	}
            }
            //Log.d("TAG","Key: " + key.toString());
		}
	}
	private void ParsePlace_News(Object obj){
		event = new Event();
		Map<String, Object> response3 = (Map<String, Object>)obj;
		for (Map.Entry<String, Object> entry3 : response3.entrySet()) {
        	String key3 = entry3.getKey();
            Object value3 = entry3.getValue();
            
            //Log.d("TAG","Value3: " + value3.toString());
            
            if(key3.equals("event_id")){
            	event.setEvent_Id(value3.toString());
            }else if(key3.equals("display_name")){
            	event.setDisplay_Name(value3.toString());
            }else if(key3.equals("description")){
            	event.setDescription(value3.toString());
            }else if(key3.equals("start_date")){
            	event.setStart_Date(value3.toString());
            }else if(key3.equals("end_date")){
            	event.setEnd_Date(value3.toString());
	      	}else if(key3.equals("event_type")){
	      		event.setEvent_Type(value3.toString());
	        }else if(key3.equals("flee")){
	        	event.setFlee(value3.toString());
	        }else if(key3.equals("web")){
				event.setWeb_Site(value3.toString());
			}else if(key3.equals("email")){
				event.setEmail(value3.toString());
			}else if(key3.equals("phone_number")){
				event.setPhone_Number(value3.toString());
			}else if(key3.equals("place_info")){
				place_info = new Place();
				Map<String, Object> response4 = (Map<String, Object>)value3;
		        for (Map.Entry<String, Object> entry4 : response4.entrySet()) {
		        	String key4 = entry4.getKey();
		            Object value4 = entry4.getValue();
		            
		            if(key4.equals("place_id")){
		            	place_info.setPlaceId(value4.toString());
		            }else if(key4.equals("email")){
		            	place_info.setEmail(value4.toString());
		            }else if(key4.equals("website")){
		            	place_info.setWebsite(value4.toString());
		            }else if(key4.equals("added_timestamp")){
		            	place_info.setAdded_timestamp(value4.toString());
		            }else if(key4.equals("description")){
		            	place_info.setDescription(value4.toString());
		            }else if(key4.equals("name")){
		            	place_info.setName(value4.toString());
			      	}else if(key4.equals("create_timestamp")){
			      		place_info.setCreate_timestamp(value4.toString());
			        }else if(key4.equals("exhibition_status")){
			        	place_info.setExhibition_status(value4.toString());
			        }
			        else if(key4.equals("location")){
			        	Location location = new Location();
			            Map<String, Object> response5 = (Map<String, Object>)value4;
					    for (Map.Entry<String, Object> entry5 : response5.entrySet()) {
					         String key5 = entry5.getKey();
						     Object value5 = entry5.getValue();								         
						        
						     if(key5.equals("longitude")){
						    	 location.setLongitude(value5.toString());
						     }else if(key5.equals("description")){
						        location.setDescription(value5.toString());
						     }else if(key5.equals("latitude")){
						        location.setLatitude(value5.toString());
						     }								        
					    }//End Loop4
					    place_info.setLocation(location);
			        }else if(key4.equals("open_date")){
			        	Open_Date opendate = new Open_Date();
			        	Map<String, Object> response5 = (Map<String, Object>)value4;
					        for (Map.Entry<String, Object> entry5 : response5.entrySet()) {
					           	String key5 = entry5.getKey();
						        Object value5 = entry5.getValue();
						        
						        Open_Date_Item opendateitem = new Open_Date_Item();
						        opendateitem.setDate(key5);
							    Map<String, Object> response6 = (Map<String, Object>)value5;
								for (Map.Entry<String, Object> entry6 : response6.entrySet()) {
									String key6 = entry6.getKey();
									Object value6 = entry6.getValue();									        
										        
									if(key6.equals("close_time")){
										opendateitem.setClose_time(value6.toString());
									}else if(key6.equals("open_time")){
										opendateitem.setOpen_time(value6.toString());
									}							
								}//End Loop5
								opendate.addItem(opendateitem);
								place_info.setOpen_date(opendate);
					        }
			          }//End if			          
		        }
		        event.setPlace_Info(place_info);
            }         
        }
        place_news.addItem(event);
	}
}
